package cn.edu.tju.elm.controller;

import cn.edu.tju.elm.model.DTO.FoodDTO;
import cn.edu.tju.elm.model.entity.Food;
import cn.edu.tju.elm.common.response.HttpResult;
import cn.edu.tju.elm.service.FoodService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/foods")
@Tag(name = "商品管理", description = "提供商品的增删改查功能，包括按商家查询商品、添加商品、更新商品信息等")
public class FoodController {
    private final FoodService foodService;

    public FoodController(FoodService foodService) {
        this.foodService = foodService;
    }

    @GetMapping("/{id}")
    @Operation(
            summary = "根据ID获取商品详情",
            description = "根据商品ID获取指定商品的详细信息"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "商品不存在")
    })
    public HttpResult<Food> getFoodById(
            @Parameter(description = "商品ID", required = true, example = "1")
            @PathVariable Long id) {
        return HttpResult.ok(foodService.getFoodById(id));
    }

    @DeleteMapping("/{id}")
    @Operation(
            summary = "删除商品",
            description = "删除指定ID的商品，通常需要商家权限"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "403", description = "权限不足，需要商家权限"),
            @ApiResponse(responseCode = "404", description = "商品不存在")
    })
    public HttpResult<Food> deleteFoodById(
            @Parameter(description = "商品ID", required = true, example = "1")
            @PathVariable Long id){
        return HttpResult.ok(foodService.deleteFoodById(id));
    }

    @GetMapping("")
    @Operation(
            summary = "根据商家ID获取商品列表",
            description = "根据店铺ID获取该店铺下的所有商品列表"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "商家不存在")
    })
    public HttpResult<List<Food>> listFoodByBusiness(
            @Parameter(description = "商家ID", required = true, example = "1")
            @RequestParam Long business) {
        return HttpResult.ok(foodService.listFoodByBusiness(business));
    }

    @PostMapping("/businesses/{businessId}")
    @Operation(
            summary = "为指定商家添加商品",
            description = "为指定的商家添加一个新的商品"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "添加成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "403", description = "权限不足，需要商家权限"),
            @ApiResponse(responseCode = "404", description = "商家不存在")
    })
    public HttpResult<Food> addFood(
            @Parameter(description = "商品信息", required = true,
                    examples = @ExampleObject(
                            value = """
                                {
                                    "foodName": "商品名",
                                    "foodExplain": "商品描述",
                                    "foodImg": "https://loremflickr.com/400/400?lock=2609054060243238",
                                    "foodPrice": 198.5,
                                    "remarks": "备注信息"
                                }
                            """
                    ))
            @RequestBody Food food,
            @Parameter(description = "商家ID", required = true, example = "1")
            @PathVariable Long businessId) {
        return HttpResult.ok(foodService.addFood(food, businessId));
    }

    @PutMapping("/{id}")
    @Operation(
            summary = "更新商品信息",
            description = "全量更新指定商品的所有信息"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "403", description = "权限不足，需要商家权限"),
            @ApiResponse(responseCode = "404", description = "商品不存在")
    })
    public HttpResult<Food> updateFood(
            @Parameter(description = "商品ID", required = true, example = "1")
            @PathVariable Long id,
            @Parameter(description = "商品更新信息", required = true)
            @RequestBody FoodDTO foodDTO) {
        return HttpResult.ok(foodService.updateFood(id, foodDTO));
    }

    @PostMapping("")
    @Operation(
            summary = "添加商品（简化版）",
            description = "添加商品的简化接口，商品信息中需要包含商家信息"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "添加成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "403", description = "权限不足，需要商家权限"),
            @ApiResponse(responseCode = "404", description = "关联商家不存在")
    })
    public HttpResult<Food> addFood2(
            @Parameter(description = "商品信息（包含商家信息）", required = true)
            @RequestBody Food food){
        return HttpResult.ok(foodService.addFood2(food));
    }
}